package com.slz3.business.module.orderManagement.ordOrder.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.orderManagement.ordOrderLine.domain.PaymentTypeEnum;
import org.springframework.util.MultiValueMap;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

public class OrdOrderFactory {

    /**
     * 初始化基础属性值
     */
    public static OrdOrder initBaseFields(OrdOrder ordOrder) {
        if (ordOrder.getUuid() == null) {
            ordOrder.setUuid(UUIDUtils.generateUUID());
            ordOrder.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(ordOrder.getSerAudit())) {
                ordOrder.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(ordOrder.getSerUsing())) {
                ordOrder.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            ordOrder.setLastModifyTime(LocalDateTime.now());
        }
        return ordOrder;
    }

    /**
     * 初始化基础属性值
     */
    public static OrdOrder initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static OrdOrder newTemStyle() {
        return new OrdOrder();
    }

    public static OrdOrder newByOrder(Order order) {
        Steward.initNullField(order);
        OrdOrder ordOrder = new OrdOrder();
        ordOrder.setUuid(UUID.fromString(order.getUuid()));
        ordOrder.setIncrementId(Long.getLong(order.getIncrementId()));
        ordOrder.setSerOrderNo(order.getSerOrderNo());
        ordOrder.setSerTotalFee(new BigDecimal(order.getSerTotalFee()));
        ordOrder.setSerUserId(UUID.fromString(order.getSerMemberUuid()));
        ordOrder.setSerOrderAddressId(UUID.fromString(order.getSerOrderAddressId()));
        ordOrder.setSerMemo(order.getSerMemo());
        ordOrder.setItemNames(Optional.ofNullable(order.getItemNames()).orElse(""));
        if (ordOrder.getItemNames() != null && ordOrder.getItemNames().length() > 200) {
            ordOrder.setItemNames("...");
        }
        ordOrder.setSerOrderType(Optional.ofNullable(order.getSerOrderType()).orElse(""));
        ordOrder.setUserName(Optional.ofNullable(order.getUserName()).orElse(""));
        ordOrder.setReceiverName(Optional.ofNullable(order.getReceiverName()).orElse(""));
        ordOrder.setReceiverPhone(Optional.ofNullable(order.getReceiverPhone()).orElse(""));
        ordOrder.setAddressStr(Optional.ofNullable(order.getAddressStr()).orElse(""));
        ordOrder.setAddressDetail(Optional.ofNullable(order.getAddressDetail()).orElse(""));
        if (VE.isNotEmpty(order.getSerCreateTime()))
            ordOrder.setCreateTime(LocalDT.parseStringToDateTime(order.getSerCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        if (VE.isNotEmpty(order.getSerEndTime()))
            ordOrder.setSerEndTime(LocalDT.parseStringToDateTime(order.getSerEndTime(), "yyyy-MM-dd HH:mm:ss"));
        if (VE.isNotEmpty(order.getSerCloseTime()))
            ordOrder.setSerCloaseTime(LocalDT.parseStringToDateTime(order.getSerCloseTime(), "yyyy-MM-dd HH:mm:ss"));
        if (VE.isNotEmpty(order.getSerDeliveryTime()))
            ordOrder.setSerDeliveryTime(LocalDT.parseStringToDateTime(order.getSerDeliveryTime(), "yyyy-MM-dd HH:mm:ss"));
        if (VE.isNotEmpty(order.getSerPaymentTime()))
            ordOrder.setSerPaymentTime(LocalDT.parseStringToDateTime(order.getSerPaymentTime(), "yyyy-MM-dd HH:mm:ss"));
        ordOrder.setSerPayNo(order.getSerPayNo());
        ordOrder.setSerPayType("1".equals(order.getSerPayType()) ? PayTypeEnum.JIFENG : "2".equals(order.getSerPayType()) ? PayTypeEnum.MONEY : null);
        ordOrder.setSerCloseReason(Optional.ofNullable(order.getSerCloseReason()).orElse(""));
        if (VE.isNotEmpty(order.getSerOrderStatus()))
            ordOrder.setSerOrderStatus(Integer.valueOf(order.getSerOrderStatus()));
        switch (order.getSerPaymentType()) {
            case "1":
                ordOrder.setSerPaymentType(PaymentTypeEnum.ALYAPY);
                break;
            case "2":
                ordOrder.setSerPaymentType(PaymentTypeEnum.WEIXIN);
                break;
            case "3":
                ordOrder.setSerPaymentType(PaymentTypeEnum.YINLIAN);
                break;
            case "4":
                ordOrder.setSerPaymentType(PaymentTypeEnum.JIFEN);
                break;
            default:
                ordOrder.setSerPaymentType(null);
                break;
        }
        return ordOrder;
    }

    public static OrdOrderCondition newConditionByMultiValueMap(MultiValueMap<String, String> params) {
        OrdOrderCondition condition = new OrdOrderCondition();

        String uuid = params.getFirst("uuid");
        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serTotalFeeLeft = params.getFirst("serTotalFeeLeft");
        String serTotalFeeRight = params.getFirst("serTotalFeeRight");
        List<String> orderStatusList = params.get("orderStatusList");
        List<String> serOrderTypeList = params.get("serOrderTypeList");

        try {
            condition.setUuid(uuid == null ? null : UUID.fromString(uuid));
            condition.setSerTotalFeeLeft(serTotalFeeLeft == null ? null : new BigDecimal(serTotalFeeLeft));
            condition.setSerTotalFeeRight(serTotalFeeLeft == null ? null : new BigDecimal(serTotalFeeRight));
        } catch (Exception e) {
        }
        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, OrdOrderCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, OrdOrderCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, OrdOrderCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, OrdOrderCondition.CREATE_TIME_OF_PATTERN));
        condition.setOrderStatusList(orderStatusList == null ? null : orderStatusList.stream().map(item -> Integer.valueOf(item)).collect(Collectors.toList()));
        condition.setSerOrderTypeList(serOrderTypeList);
        return condition;
    }
}
